package com.fireline.beauty.seting;

import java.text.DecimalFormat;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.fireline.beauty.bean.FwxmVo;
import com.fireline.beauty.jdbc.SqliteJdbcImpl;
import com.fireline.beauty.test.CnToSpell;
import com.zgw.fireline.base.controls.DataTable;
import com.zgw.fireline.base.controls.DataTableColumn;
import com.zgw.fireline.base.dataset.Dataset;
import com.zgw.fireline.base.frame.IFunction;
import com.zgw.fireline.base.widgets.Batch;
import com.zgw.fireline.base.widgets.BatchItem;

/**
 * 套餐设置编辑
 * 
 * */
public class TcszEdit implements IFunction {
	public static final int Add = 1;
	public static final int Edit = 2;
	private Text textTcbh;
	private Text textTcmc;
	private Text textSj;
	private Text textBz;
	private int type = 1;
	public int result = 0;

	public TcszEdit(int type, Dataset set) {
		this.type = type;
		if (type == Edit) {
			tcxxSet.populate(set, set.getSelectedIndex());
			tcxxSet.setSelected(0);
		}
	}

	/**
	 * @wbp.nonvisual location=27,502
	 */
	private final Dataset tcxxSet = new Dataset(true, "tcxx",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=107,502
	 */
	private final Dataset fwxm = new Dataset(true, "tcfwxm",
			SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=23,612
	 */
	private final Batch cpxxEdit = new Batch(SqliteJdbcImpl.INSTANCE);
	/**
	 * @wbp.nonvisual location=103,612
	 */
	private final Batch cpxxAdd = new Batch(SqliteJdbcImpl.INSTANCE);
	private Composite content;
	private DataTable tableTzcp;
	/**
	 * @wbp.nonvisual location=277,502
	 */
	private final Dataset newId = new Dataset(
			"select max(tcbh)+1 newId from tcxx\r\nwhere length(tcbh)=3\r\n",
			SqliteJdbcImpl.INSTANCE);
	private Button btnCheckButton;
	private Text textYxq;

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	public Control createControl(Composite parent) {
		tcxxSet.setKeyColumns(new String[] {});
		fwxm.setKeyColumns(new String[] {});
		{
			BatchItem batchItem = new BatchItem(cpxxEdit);
			batchItem
					.setCommand("UPDATE tcxx\r\nSET  jm=? ,tcmc=? , yxq=?,\r\nsj=?,  bz=?,fwxm=?\r\nWHERE tcbh=?");
			batchItem.setName("修改套餐基本信息");
		}
		{
			BatchItem batchItem = new BatchItem(cpxxEdit);
			batchItem.setCommand("delete from tcfw where tcbh=?");
			batchItem.setName("删除对应的服务项目");
		}
		newId.setKeyColumns(new String[] {});
		{
			BatchItem batchItem = new BatchItem(cpxxAdd);
			batchItem
					.setCommand("insert into tcxx(tcbh,jm,tcmc,yxq,sj,bz,fwxm)\r\nvalues(?,?,?,?,?,?,?)");
			batchItem.setName("新增产品信息");
		}
		{
			BatchItem batchItem = new BatchItem(cpxxAdd);
			batchItem.setCommand(" \r\n delete from tcfw where tcbh=?");
			batchItem.setName("删除对应的套餐服务");
		}
		final Composite content = new Composite(parent, SWT.NONE);
		content.setLayout(new GridLayout(1, false));

		Group composite = new Group(content, SWT.NONE);
		composite.setText("产品基本信息");
		GridLayout gl_composite = new GridLayout(4, false);
		gl_composite.verticalSpacing = 15;
		gl_composite.horizontalSpacing = 0;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false,
				1, 1));
		{
			Label lblNewLabel = new Label(composite, SWT.NONE);
			lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER,
					false, false, 1, 1));
			lblNewLabel.setText("套餐编号：");
		}
		{
			textTcbh = new Text(composite, SWT.BORDER);
			textTcbh.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
		}
		{
			Label label = new Label(composite, SWT.NONE);
			{
				GridData gd_label = new GridData(SWT.RIGHT, SWT.CENTER, false,
						false, 1, 1);
				gd_label.horizontalIndent = 20;
				label.setLayoutData(gd_label);
			}
			label.setText("套餐名称：");
		}
		{
			textTcmc = new Text(composite, SWT.BORDER);
			textTcmc.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
		}
		{
			Label label = new Label(composite, SWT.NONE);
			label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
					false, 1, 1));
			label.setText("售价：");
		}
		{
			textSj = new Text(composite, SWT.BORDER);
			textSj.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
		}
		{
			Label label = new Label(composite, SWT.NONE);
			label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
					false, 1, 1));
			label.setText("有效期：");
		}
		{
			textYxq = new Text(composite, SWT.BORDER);
			textYxq.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 1, 1));
		}
		{
			Label label = new Label(composite, SWT.NONE);
			label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
					false, 1, 1));
			label.setText("备注信息：");
		}
		{
			textBz = new Text(composite, SWT.BORDER);
			textBz.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
					false, 3, 1));
		}

		Group group = new Group(content, SWT.NONE);
		group.setLayout(new GridLayout(2, false));
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		group.setText("套餐包含服务项目列表");
		{
			tableTzcp = new DataTable(group, SWT.BORDER | SWT.FULL_SELECTION,
					fwxm);
			tableTzcp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
					true, 1, 1));
			tableTzcp.setHeaderVisible(true);
			tableTzcp.setLinesVisible(true);
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.NONE, "xmbh");
				dataTableColumn.setText("项目编号");
				dataTableColumn.setWidth(70);
			}
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.NONE, "xmmc");
				dataTableColumn.setText("项目名称");
				dataTableColumn.setWidth(80);
			}
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.RIGHT, "dj");
				dataTableColumn.setText("原价");
				dataTableColumn.setWidth(50);
			}
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.RIGHT, "cs");
				dataTableColumn.setText("服务次数");
				dataTableColumn.setWidth(70);
			}
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.RIGHT, "yhdj");
				dataTableColumn.setText("优惠价");
				dataTableColumn.setWidth(60);
			}
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.NONE, "hj");
				dataTableColumn.setText("合计");
				dataTableColumn.setWidth(50);
			}
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.NONE, "zq");
				dataTableColumn.setText("周期");
				dataTableColumn.setWidth(50);
			}
			{
				DataTableColumn dataTableColumn = new DataTableColumn(
						tableTzcp, SWT.NONE, "bz");
				dataTableColumn.setText("备注");
				dataTableColumn.setWidth(80);
			}
		}
		{
			ToolBar toolBar = new ToolBar(group, SWT.FLAT | SWT.RIGHT
					| SWT.VERTICAL);
			toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, true,
					1, 1));
			{
				ToolItem tltmNewItem = new ToolItem(toolBar, SWT.NONE);
				tltmNewItem.setText("添加");
				tltmNewItem.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						doAddtcfw();
					};
				});
			}
			{
				ToolItem tltmNewItem_3 = new ToolItem(toolBar, SWT.NONE);
				tltmNewItem_3.setText("修改");
			}
			{
				ToolItem tltmNewItem_2 = new ToolItem(toolBar, SWT.NONE);
				tltmNewItem_2.setText("删除");
			}
		}
		{
			Label lblNewLabel_1 = new Label(group, SWT.NONE);
			{
				GridData gd_lblNewLabel_1 = new GridData(SWT.LEFT, SWT.CENTER,
						false, false, 1, 1);
				gd_lblNewLabel_1.horizontalIndent = 5;
				lblNewLabel_1.setLayoutData(gd_lblNewLabel_1);
			}
			lblNewLabel_1.setForeground(SWTResourceManager
					.getColor(SWT.COLOR_BLUE));
			lblNewLabel_1.setText("由 0 个服务组成套餐，总价值 0 元");
		}
		new Label(group, SWT.NONE);

		Composite composite_1 = new Composite(content, SWT.NONE);
		composite_1.setLayout(new GridLayout(2, false));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		btnCheckButton = new Button(composite_1, SWT.CHECK);
		btnCheckButton.setText("保存后继续添加");
		btnCheckButton.setVisible(type == Add);
		Composite composite_2 = new Composite(composite_1, SWT.NONE);
		composite_2.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true,
				false, 1, 1));

		Button btnNewButton_1 = new Button(composite_2, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				doSave();
			}
		});
		btnNewButton_1.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton_1.setText("保存");

		Button btnNewButton = new Button(composite_2, SWT.NONE);
		btnNewButton.setLayoutData(new RowData(80, SWT.DEFAULT));
		btnNewButton.setBounds(0, 0, 72, 22);
		btnNewButton.setText("退出");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				result = SWT.CANCEL;
				content.dispose();
			}
		});
		setParams();
		setValues();
		createGUIAfter();
		this.content = content;
		return content;
	}

	// 添加套餐服务
	protected void doAddtcfw() {
		TcfwxmDlg dlg = new TcfwxmDlg(content.getShell(), SWT.NONE);
		if (dlg.open() == SWT.OK) {
			TableItem item = new TableItem(tableTzcp, SWT.NONE);
			FwxmVo fwxm = dlg.fwxm;
			item.setText(new String[] { fwxm.xmbh, fwxm.xmmc, fwxm.dj,
					"" + fwxm.fwcs, fwxm.yhdj, fwxm.ssje, "" + fwxm.hlzq,
					fwxm.bz });
			item.setData(fwxm);
			tableTzcp.setSelection(item);
		}
	}

	protected void doSave() {
		String fwxm = "";
		for (TableItem i : tableTzcp.getItems()) {
			if (tableTzcp.indexOf(i) != 0)
				fwxm += ",";
			fwxm += i.getText(1);
		}
		if (type == Edit) {
			setParams();
			// 设置简码 转换成汉字拼音首字母
			cpxxEdit.setParam(0, 0,
					CnToSpell.getPinYinHeadChar(textTcmc.getText()));
			cpxxEdit.setParam(0, 5, fwxm);
			// 设置服务项目

			// 生成套装产品的添加处理项
			for (TableItem item : tableTzcp.getItems()) {
				BatchItem batc = new BatchItem(cpxxEdit);
				batc.setCommand("insert into tcfw(tcbh,xmbh,cs,yhdj,zq,bz) values(?,?,?,?,?,?)");
				batc.setName("添加套餐服务项目");
				batc.setParam(0, textTcbh.getText()); // 套装编号
				batc.setParam(1, item.getText(0)); // 项目编号
				batc.setParam(2, item.getText(3)); // 次数
				batc.setParam(3, item.getText(4)); // 优惠单价
				batc.setParam(4, item.getText(6)); // 周期
				batc.setParam(5, item.getText(7)); // 备注
			}
			cpxxEdit.execute();
			result = SWT.OK;
			content.dispose();
		} else if (type == Add) {
			setParams();
			cpxxAdd.setParam(0, 1,
					CnToSpell.getPinYinHeadChar(textTcmc.getText()));
			cpxxAdd.setParam(0, 6, fwxm);
			// 生成套装产品的添加处理项
			for (TableItem item : tableTzcp.getItems()) {
				BatchItem batc = new BatchItem(cpxxEdit);
				batc.setCommand("insert into tcfw(tcbh,xmbh,cs,yhdj,zq,bz) values(?,?,?,?,?,?)");
				batc.setName("添加套餐服务项目");
				batc.setParam(0, textTcbh.getText()); // 套装编号
				batc.setParam(1, item.getText(0)); // 项目编号
				batc.setParam(2, item.getText(3)); // 次数
				batc.setParam(3, item.getText(4)); // 优惠单价
				batc.setParam(4, item.getText(6)); // 周期
				batc.setParam(5, item.getText(7)); // 备注
			}
			cpxxAdd.execute();
			result = SWT.OK;
			content.dispose();
		}
	}

	protected void setParams() {
		cpxxEdit.setParam(0, 0, "");
		cpxxEdit.setParam(0, 1, textTcmc.getText());
		cpxxEdit.setParam(0, 2, textYxq.getText());
		cpxxEdit.setParam(0, 3, textSj.getText());
		cpxxEdit.setParam(0, 4, textBz.getText());
		cpxxEdit.setParam(0, 5, "");
		cpxxEdit.setParam(0, 6, textTcbh.getText());
		cpxxEdit.setParam(1, 0, textTcbh.getText());
		cpxxAdd.setParam(0, 0, textTcbh.getText());
		cpxxAdd.setParam(0, 1, "");
		cpxxAdd.setParam(0, 2, textTcmc.getText());
		cpxxAdd.setParam(0, 3, textYxq.getText());
		cpxxAdd.setParam(0, 4, textSj.getText());
		cpxxAdd.setParam(0, 5, textBz.getText());
		cpxxAdd.setParam(0, 6, "");
		cpxxAdd.setParam(1, 0, textTcbh.getText());
		fwxm.setParam(0, textTcbh.getText());
	}

	protected void createGUIAfter() {
		setParams();
		fwxm.update();
	}

	protected void setValues() {
		textTcbh.setText(tcxxSet.getValue("tcbh", String.class, ""));
		textTcmc.setText(tcxxSet.getValue("tcmc", String.class, ""));
		textSj.setText(tcxxSet.getValue("sj", String.class, ""));
		textYxq.setText(tcxxSet.getValue("yxq", String.class, ""));
		textBz.setText(tcxxSet.getValue("bz", String.class, ""));
		if (type == Add) { // 设置产品编号
			newId.update();
			if (newId.getCount() > 0) {
				Integer id = newId.getValueForIndex(0, "newId", Integer.class);
				DecimalFormat df = new DecimalFormat("000");
				textTcbh.setText(df.format(id));
				textTcbh.selectAll();
			}
		}
	}
}
